package com.zm.rpc.server.channel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zm.rpc.server.channel.handler.RpcDecoder;
import com.zm.rpc.server.channel.handler.RpcEncoder;
import com.zm.rpc.server.channel.handler.RpcHandler;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;

@Component
public class RpcServerChannelInitializer extends ChannelInitializer<SocketChannel>{

    @Autowired
    private RpcHandler rpcHandler;

    @Override
    protected void initChannel(SocketChannel sch) throws Exception {
        sch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,12,4,0,0));
        sch.pipeline().addLast(new RpcDecoder());
        sch.pipeline().addLast(new RpcEncoder());
        sch.pipeline().addLast(rpcHandler);
    }
    
}
